package com.sanpu.kxe.business.trade.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.trade.adapter.AllClassifyAdapter;
import com.sanpu.kxe.business.trade.adapter.RebTotalListAdapter;
import com.sanpu.kxe.business.trade.bean.BillTypeOutput;
import com.sanpu.kxe.business.trade.bean.RebTotalOutput;
import com.sanpu.kxe.business.trade.contract.RebTotalListContract;
import com.sanpu.kxe.business.trade.presenter.RebTotalListPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.utils.DateCheckPopListener;
import com.sanpu.kxe.common.view.NoScrollGridView;
import com.sanpu.kxe.jump.eventbus.FilterEvent;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.wheel.interfaces.IWheelDataChangeCallback;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：返佣明细
 * Created by 薛勇军 on 2019/4/16.
 */

public class RebTotalListActivity extends BaseTitleActivity implements RebTotalListContract.View {

    @Bind(R.id.list_view)
    RecyclerView vListView;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.emptyView)
    View emptyView;
    @Bind(R.id.allClassifyView)
    View allClassifyView;

    @Bind(R.id.allClassify)
    TextView allClassify;
    @Bind(R.id.allArrowIv)
    ImageView allArrowIv;

    private LinearLayout allClassifyLl;

    private RoundTextView startDateRtv, endDateRtv;

    private RebTotalListAdapter mListAdapter;

    private AllClassifyAdapter mClassifyAdapter;

    private RebTotalListContract.Presenter mPresenter;

    private Animation loadInAnimation, loadOutAnimation;

    private FilterEvent mFilterEvent;

    private DateCheckPopListener vCheckPopListener;

    private boolean isSelectAll = false;

    private int nowPage = 0;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_rebtotal_list;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("收益明细");
        vListView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mListAdapter = new RebTotalListAdapter();
        vListView.setAdapter(mListAdapter);
        mListAdapter.disableLoadMoreIfNotFullPage(vListView);

        allClassifyLl = ButterKnife.findById(allClassifyView, R.id.allClassifyLl);
        NoScrollGridView allClassifyGv = ButterKnife.findById(allClassifyView, R.id.allClassifyGv);
        startDateRtv = ButterKnife.findById(allClassifyView, R.id.startDateRtv);
        endDateRtv = ButterKnife.findById(allClassifyView, R.id.endDateRtv);
        mClassifyAdapter = new AllClassifyAdapter(mContext);
        allClassifyGv.setAdapter(mClassifyAdapter);
        allClassifyGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mClassifyAdapter.setNotifyData(position);
                BillTypeOutput currentBillType = ((AllClassifyAdapter)parent.getAdapter()).getItem(position);
                if (currentBillType != null && !TextUtils.isEmpty(currentBillType.getName())) {
//                    allClassify.setText(currentBillType.getName());
                    if (mFilterEvent == null)
                        mFilterEvent = new FilterEvent();
                    mFilterEvent.setTransactionType(currentBillType.getType());
                    EventBus.getDefault().post(mFilterEvent);
                }
            }
        });
        ButterKnife.findById(allClassifyView, R.id.comfirmRtv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAllClassifyPop(true);
                EventBus.getDefault().post(mFilterEvent);
            }
        });

        mPresenter = new RebTotalListPresenter(this);
        mPresenter.myHstRebateList(nowPage, mFilterEvent);
        mPresenter.getRebateType();
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = 0;
                mPresenter.myHstRebateList(nowPage, mFilterEvent);
                mListAdapter.setEnableLoadMore(true);
            }
        });
        mListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.myHstRebateList(nowPage, mFilterEvent);
            }
        }, vListView);
        mListAdapter.setPreLoadNumber(2);

        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nowPage = 0;
                mPresenter.myHstRebateList(nowPage, mFilterEvent);
                mListAdapter.setEnableLoadMore(true);
            }
        });
        ButterKnife.findById(allClassifyView, R.id.emptyView).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isRespondEvent())
                    showAllClassifyPop(true);
                return true;
            }
        });
        allClassifyView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        startDateRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vCheckPopListener == null) {
                    vCheckPopListener = new DateCheckPopListener(RebTotalListActivity.this);
                }
                vCheckPopListener.handleRegionCheck(v, startDateRtv, true, new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            startDateRtv.setText(wheelData);
                        }
                        if (mFilterEvent == null)
                            mFilterEvent = new FilterEvent();
                        mFilterEvent.setStartTime(wheelData);
                    }
                });
            }
        });
        endDateRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vCheckPopListener == null) {
                    vCheckPopListener = new DateCheckPopListener(RebTotalListActivity.this);
                }
                vCheckPopListener.handleRegionCheck(v, endDateRtv, true, new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            endDateRtv.setText(wheelData);
                        }
                        if (mFilterEvent == null)
                            mFilterEvent = new FilterEvent();
                        mFilterEvent.setEndTime(wheelData);
                    }
                });
            }
        });
    }

    @OnClick({R.id.allClassifyLayout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.allClassifyLayout:
                showAllClassifyPop(isSelectAll);
                break;
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefresh(FilterEvent event) {
        if (event != null) {
            mFilterEvent = event;
            mListAdapter.setNewData(new ArrayList<RebTotalOutput>());
            nowPage = 0;
            mPresenter.myHstRebateList(nowPage, mFilterEvent);
        }
    }

    /**
     * 显示全部分类pop
     */
    private void showAllClassifyPop(boolean selectAll) {
        if (selectAll) {
            isSelectAll = false;
            allClassify.setTextColor(ContextCompat.getColor(mContext, R.color.c_909090));
            allArrowIv.setImageResource(R.drawable.ic_bottom_arrow);
            loadOutAnimation = AnimationUtils.loadAnimation(mContext,
                    R.anim.push_top_out);
            loadOutAnimation.setFillAfter(true);
            allClassifyLl.startAnimation(loadOutAnimation);
            loadOutAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    allClassifyView.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        } else {
            isSelectAll = true;
            allClassify.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
            allArrowIv.setImageResource(R.drawable.ic_top_arrow);
            allClassifyView.setVisibility(View.VISIBLE);
            loadInAnimation = AnimationUtils.loadAnimation(mContext,
                    R.anim.push_top_in);
            loadInAnimation.setFillAfter(true);
            allClassifyLl.startAnimation(loadInAnimation);
        }
    }

    @Override
    public void showRebTotalView(List<RebTotalOutput> totalOutputs, boolean isFirst) {
        if (totalOutputs != null && totalOutputs.size()>0) {
            vSwipeRefreshLayout.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            if (isFirst) {
                mListAdapter.setNewData(totalOutputs);
            } else {
                mListAdapter.addData(totalOutputs);
            }
            nowPage++;
        } else if (isFirst) {
            vSwipeRefreshLayout.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                mListAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                mListAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                mListAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    public void showBillTypeView(List<BillTypeOutput> billTypeOutputs) {
        BillTypeOutput retDatBean = new BillTypeOutput();
        retDatBean.setName("全部分类");
        retDatBean.setType("");
        if (billTypeOutputs == null)
            billTypeOutputs = new ArrayList<>();
        billTypeOutputs.add(0, retDatBean);
        mClassifyAdapter.setNotifyData(billTypeOutputs);
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(RebTotalListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
